package com.ktgj.customer.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.vo.ItineraryVo;
import com.ktgj.customer.service.ILoginService;
import com.ktgj.flight.api.domain.FightStopReqVoApi;
import com.ktgj.flight.api.domain.FightStopResVoApi;
import com.ktgj.flight.api.domain.FlightResponseVoApi;
import com.ktgj.flight.api.domain.MoFlightSearchRequestApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.customer.domain.Itinerary;
import com.ktgj.customer.service.IItineraryService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 行程定制Controller
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@ControllerAdvice
@RestController
@RequestMapping("/itinerary")
public class ItineraryController extends BaseController
{
    @Autowired
    private IItineraryService itineraryService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询行程定制列表
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:list")
    @GetMapping("/list")
    public TableDataInfo list(Itinerary itinerary)
    {
        startPage();
        List<Itinerary> list = itineraryService.selectItineraryList(itinerary);
        return getDataTable(list);
    }

    /**
     * 导出行程定制列表
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:export")
    @Log(title = "行程定制", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Itinerary itinerary) throws IOException
    {
        List<Itinerary> list = itineraryService.selectItineraryList(itinerary);
        ExcelUtil<Itinerary> util = new ExcelUtil<Itinerary>(Itinerary.class);
        util.exportExcel(response, list, "行程定制数据");
    }

    /**
     * 获取行程定制详细信息
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:query")
    @GetMapping(value = "/{itinerary}")
    public AjaxResult getInfo(@PathVariable("itinerary") Long itinerary)
    {
        return AjaxResult.success(itineraryService.selectItineraryById(itinerary));
    }

    /**
     * 新增行程定制
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:add")
    @Log(title = "行程定制", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Itinerary itinerary)
    {
        return toAjax(itineraryService.insertItinerary(itinerary));
    }

    /**
     * 修改行程定制
     */
    //@PreAuthorize(hasPermi = "customer:itinerary:edit")
    @Log(title = "行程定制", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Itinerary itinerary)
    {
        return toAjax(itineraryService.updateItinerary(itinerary));
    }

    /**
     * 删除行程定制
     */
	@PostMapping("/remove")
    public AjaxResult remove(@RequestBody Map<String, Object> map) {
	    Long id = Long.valueOf(map.get("id").toString());
        int i = itineraryService.deleteItineraryById(id);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }

    @GetMapping("/select")
    public AjaxResult selectItineraryItineraryOrder(Long itinerary){
        return AjaxResult.success(itineraryService.selectItineraryItineraryOrder(itinerary));
    }

    /**
     * 评价中心
     * */
    //@SignatureValidation
    @PostMapping("/evaluationCenter")
    public AjaxResult evaluationCenter(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        String evaluation = map.get("evaluation").toString();
        List<ItineraryVo> itineraryVoList = itineraryService.selectItineraryByCustomerId(customerId, evaluation);
        if (itineraryVoList.size() > 0){
            return AjaxResult.success(itineraryVoList);
        }
        else {
            return AjaxResult.error("false");
        }
    }

    /**
     * 评价行程
     * */
    //@SignatureValidation
    @PostMapping("/journeyEvaluation")
    public AjaxResult journeyEvaluation(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        Long itineraryCustomizeId = Long.valueOf(map.get("itinerary").toString());
        String itineraryAppraise = map.get("itineraryAppraise").toString();
        String itineraryAppraiseContent = map.get("itineraryAppraiseContent").toString();
        String dropOff = map.get("dropOff").toString();
        String vipRoom = map.get("vipRoom").toString();
        String flight = map.get("flight").toString();
        String pickUp = map.get("pickUp").toString();
        String anonymous = map.get("anonymous").toString();
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        ItineraryVo itineraryVo = new ItineraryVo();
        itineraryVo.setItineraryCustomizeId(itineraryCustomizeId);
        itineraryVo.setItineraryAppraise(itineraryAppraise);
        itineraryVo.setItineraryAppraiseContent(itineraryAppraiseContent);
        itineraryVo.setDropOff(dropOff);
        itineraryVo.setVipRoom(vipRoom);
        itineraryVo.setFlight(flight);
        itineraryVo.setPickUp(pickUp);
        itineraryVo.setAnonymous(anonymous);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        itineraryVo.setJourneyTime(format.format(new Date()));
        int count = itineraryService.updateItineraryByItinerary(itineraryVo, customerId);
        if (count > 0){
            return toAjax(count);
        }
        else {
            return AjaxResult.error("false");
        }
    }

    /**
     * 查看评价
     * */
    //@SignatureValidation
    @PostMapping("/selectEvaluation")
    public AjaxResult selectEvaluation(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Long itineraryCustomizeId = Long.valueOf(map.get("itinerary").toString());
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(itineraryService.selectItineraryByitinerary(itineraryCustomizeId, customerId));
    }

    /**
     * 删除评价
     * */
    @PostMapping("/deleteEvaluation")
    public AjaxResult deleteEvaluation(@RequestBody Map<String, Object> map){
        Long itinerary = Long.valueOf(map.get("itinerary").toString());
        return toAjax(itineraryService.updateItineraryByitinerary(itinerary));
    }

    /**
     * 定制行程-单程
     * @param request
     * @return
     */
    @PostMapping("/queryItineraryByCustomerId")
    public AjaxResult queryItineraryByCustomerId(HttpServletRequest request) {
        String token = request.getHeader("token");
        return AjaxResult.success(itineraryService.queryItineraryByCustomerId(token));
    }

    /**
     * 行程-历史行程
     * @param request
     * @return
     */
    //@SignatureValidation
    @PostMapping("/queryHistoryIyinerary")
    public AjaxResult queryHistoryIyinerary(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long customerId = loginService.getLoginCustomerByToken(token).getCustomerId();
        Map<String, Object> map = itineraryService.queryHistoryIyinerary(token, customerId);
        return AjaxResult.success(map);
    }

    /**
     * 查看行程
     * @param request
     * @param map
     * @return
     */
    //@SignatureValidation
    @PostMapping("/queryItineraryByCustomerIds")
    public AjaxResult queryItineraryByCustomerIds(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        Long itinerary = Long.valueOf(map.get("itinerary").toString());
        Map<String, Object> itineraryByCustomerIds = itineraryService.queryItineraryByCustomerIds(customerId, itinerary);
        return AjaxResult.success(itineraryByCustomerIds);
    }

    /**
     * 行程-已订票-按起降地
     * @param request
     * @return
     */
    @PostMapping("/queryFlightList")
    public AjaxResult queryFlightList(@RequestBody MoFlightSearchRequestApi request) {
        List<FlightResponseVoApi> flightList = itineraryService.queryFlightList(request);
        return AjaxResult.success(flightList);
    }

    /**
     * 行程-已订票-按航班号
     * @param map
     * @return
     */
    @PostMapping("/getFightInfoByAirCodeAndDate")
    public AjaxResult getFightInfoByAirCodeAndDate(@RequestBody Map<String, Object> map) {
        String flightNo = String.valueOf(map.get("flightNo"));
        String date = String.valueOf(map.get("date"));
        FightStopReqVoApi vo = new FightStopReqVoApi();
        vo.setCode(flightNo.substring(0, 2));
        vo.setDate(date);
        vo.setFlightNo(flightNo.substring(2));
        FightStopResVoApi fightInfoByAirCodeAndDate = itineraryService.getFightInfoByAirCodeAndDate(vo);
        return AjaxResult.success(fightInfoByAirCodeAndDate);
    }

    /**
     * 新建行程
     * */
    @PostMapping("/addItinerary")
    public AjaxResult addItinerary(HttpServletRequest request, @RequestBody Itinerary itinerary){
        Customer customer = loginService.getLoginCustomer(request);
        if (itinerary != null){
            itinerary.setCustomerId(customer.getCustomerId());
            itinerary.setContact(customer.getCustomerName());
            itinerary.setContactPhone(customer.getContractPhoneOne());
            Long id;
            try {
                id = itineraryService.addItinerary(itinerary);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (id != null && id != 0){
                return AjaxResult.success(id);
            }
            else {
                return AjaxResult.error("行程创建失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
}
